package great.easychat.help.dialog;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.IBinder;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import great.easychat.help.R;

/**
 * Created by SuperMan on 2018/08/13 0013.
 */

public abstract class BasePopup extends PopupWindow {

    public View view;
    public View maskView;
    private WindowManager windowManager;

    public Context mContext;

    public BasePopup(Context context) {
        super(context);
        mContext = context;
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        view = getView();
        this.setContentView(view);
        //自定义基础，设置我们显示控件的宽，高，焦点，点击外部关闭PopupWindow操作
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        setAnimationStyle(R.style.anim_chat_link_pup);
        //更新试图
//        this.update();
        // 不设置背景可以屏蔽back键
        ColorDrawable colorDrawable = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(colorDrawable);
    }

    public void show(Activity activity) {
        addMask(activity.getWindow().getDecorView().findViewById(android.R.id.content).getWindowToken());
        this.showAtLocation(activity.getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(maskView, "Alpha", 0, 1.0f);
        objectAnimator.setDuration(200);
        objectAnimator.start();
    }

    // 默认半透明背景
    private void addMask(IBinder token) {
        WindowManager.LayoutParams wl = new WindowManager.LayoutParams();
        wl.width = WindowManager.LayoutParams.MATCH_PARENT;
        wl.height = WindowManager.LayoutParams.MATCH_PARENT;
        wl.format = PixelFormat.TRANSLUCENT;//不设置这个弹出框的透明遮罩显示为黑色
        wl.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;//该Type描述的是形成的窗口的层级关系
        wl.token = token;//获取当前Activity中的View中的token,来依附Activity
        maskView = new View(mContext);
        maskView.setBackgroundColor(0x7f000000);
        maskView.setFitsSystemWindows(false);
        /**
         * 通过WindowManager的addView方法创建View
         * 产生出来的View根据WindowManager.LayoutParams属性不同，效果也就不同了。
         * 比如创建系统顶级窗口，实现悬浮窗口效果！
         */
        windowManager.addView(maskView, wl);
    }

    private void removeMask() {
        if (null != maskView) {
            windowManager.removeViewImmediate(maskView);
            maskView = null;
        }
    }

    @Override
    public void dismiss() {
        final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(maskView, "Alpha", 1.0f, 0);
        objectAnimator.setDuration(200);
        objectAnimator.start();
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeMask();
            }
        });
        super.dismiss();
    }

    public abstract View getView();

}
